import { _decorator, Component, Node, Sprite } from 'cc';
import { ResourceManager } from '../Tools/Manager/ResourceManager';
import { PoolObject } from '../Tools/Pool/PoolObject';
const { ccclass, property } = _decorator;

@ccclass('ProgressBar')
export class ProgressBar extends PoolObject {

    @property(Sprite)
    public fill:Sprite

    public onCompleted:()=>void;

    private progressTimer:number;

    private progressTime:number

    private fillEnd:number;

    public excute(time:number,end:number, call:()=>void){
        this.progressTime = time;
        this.progressTimer = 0;
        this.fillEnd = end;
        this.onCompleted = call;
    }

    update(deltaTime: number) {
        if(this.onCompleted!=null){
            this.progressTimer += deltaTime;
            this.fill.fillRange =  this.fillEnd *(this.progressTimer / this.progressTime );
            if(this.progressTimer >= this.progressTime)
            {
                this.progressTimer = 0;
                if (this.onCompleted) {
                    this.onCompleted();
                }
                this.onCompleted = null; // 执行后清空回调
                
                //@Daan
                ResourceManager.despawn(this,null);
                
            }
        }
    }

    
    public override onSpawn() {
        super.onSpawn();
        this.onCompleted = null;
        this.fill.fillRange = 0;
        this.progressTimer = 0;
    }
}


